package gbench.sandbox.nsql.mongo;

import org.junit.jupiter.api.Test;

import java.util.stream.Collectors;

import gbench.common.matlib.data.DataReader.DFrame;
import gbench.common.matlib.rdd.BasePair;
import gbench.common.matlib.rdd.PVec;
import gbench.common.tree.LittleTree;
import gbench.commonApp.data.DataApp;
import gbench.data.nosql.mongo.MgClc;

import static gbench.common.matlib.MatlibCanvas.println;
import static gbench.common.tree.LittleTree.IRecord.REC;
import static gbench.common.tree.LittleTree.Jdbc.XJdbcSession.OPEN;

/**
 * 
 * @author gbench
 *
 */
public class JunitProducts {
    
    /**
     * 
     * 产品展示:
     * 
     * mongo:
     * //Copy from NoSQLBooster for MongoDB free edition. This message does not appear if you are using a registered version. 
     * 
     * {
     * 	"_id" : ObjectId("5e9817ada7b32c02b2da940d"),
     * 	"Id" : NumberInt(1152),
     * 	"PctAttrsId" : NumberInt(1),
     * 	"ProductId" : NumberInt(789),
     * 	"Owner" : NumberInt(13),
     * 	"Fields" : {
     * 		"en" : "DIETHYLENE GLYCOL",
     * 		"id" : NumberInt(789),
     * 		"cas" : "111-46-6",
     * 		"code" : "",
     * 		"desc" : "外观 无色或浅黄色透明液体 rn相对密度(d 204) 1.1147~1.1180 rn沸点℃ ≥241 rn沸程〔馏出90%(体积)〕时℃ ≤250 rn闪点℃ 141 ",
     * 		"name" : "二甘醇",
     * 		"unit" : "",
     * 		"alias" : " 二甘醇;2,2’-氧代二乙醇;二羟二乙醚;二乙二醇醚;一缩二乙二醇;2-羟基乙基醚(或称缩二乙二醇,或称二甘醇);2,2'-氧代二乙醇 ;",
     * 		"bill1" : "http://hp-data.oss-cn-shanghai.aliyuncs.com/thumbnail/2018-08/3908bdfb95a54f56bbbe3d209a491f0f.jpg",
     * 		"bill2" : "http://hp-data.oss-cn-shanghai.aliyuncs.com/thumbnail/2018-07/93c0404d574048a09d4792b9b1627721.jpg",
     * 		"bill3" : "http://hp-data.oss-cn-shanghai.aliyuncs.com/thumbnail/2018-06/b99b6b6111944496baed7879a2c78513.jpg",
     * 		"count" : NumberInt(0),
     * 		"order" : NumberInt(0),
     * 		"price" : null,
     * 		"online" : "1",
     * 		"remark" : null,
     * 		"status" : "1",
     * 		"sys_id" : "13",
     * 		"creator" : "8839",
     * 		"role_id" : null,
     * 		"alias_en" : "",
     * 		"goods_id" : "15",
     * 		"is_drugs" : "0",
     * 		"operator" : null,
     * 		"packSpec" : null,
     * 		"create_at" : null,
     * 		"is_danger" : "0",
     * 		"role_name" : null,
     * 		"tax_ratio" : 0.16,
     * 		"thumbnail" : "http://hp-data.oss-cn-shanghai.aliyuncs.com/thumbnail/2018-12/2cf89c3e30b8426ba67df870fa2abbae.png?x-oss-process=image/crop,x_6,y_0,w_167,h_222",
     * 		"update_at" : NumberLong("1547742308000"),
     * 		"creator_id" : "23",
     * 		"application" : "用作气体脱水剂和芳烃抽提溶剂，也用作纺织品的润滑剂、软化剂和整理剂，以及增塑剂、增湿剂、上浆剂、硝基纤维素、树脂和油脂等溶剂。",
     * 		"category_id" : "555",
     * 		"operator_id" : "992,993,994,995,996,998,999",
     * 		"qualitySpec" : null,
     * 		"molecule_num" : "106.1204",
     * 		"category_danger" : "",
     * 		"is_highly_toxic" : "0",
     * 		"up_stream_goods" : "",
     * 		"molecule_express" : "C4H10O3",
     * 		"down_stream_goods" : "",
     * 		"category_procursor" : "",
     * 		"physicochemical_property" : "外观 无色或浅黄色透明液体 rn相对密度(d 204) 1.1147~1.1180 rn沸点℃ ≥241 rn沸程〔馏出90%(体积)〕时℃ ≤250 rn闪点℃ 141 ",
     * 		"chemical_structure_diagram" : "http://hp-data.oss-cn-shanghai.aliyuncs.com/thumbnail/2018-12/87eda44f5695480e9e0fc5c1d9e46bd6.png"
     * 	},
     * 	"Status" : NumberInt(1)
     * }
     * 
     * mysql:
     * SELECT * FROM salepro.t_pct_attribute_set;
     * '1', '[{\"field\": \"name\", \"label\": \"产品名称\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"name\", \"constraints\": \"MOXI-必填-唯一,EXISTS{/plugin/api/moxi-0.0.0.1/validator/name/exists},MSG:产品名称必须全网唯一\"}, {\"field\": \"code\", \"label\": \"编码\", \"default\": \"url:/plugin/api/moxi-0.0.0.1/misc/code/cp\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"系统默认cp+数字从小到大，如遇到某已编号已被使用，系统自动跳过该编码全网唯一，cp= chemical products\", \"valtype\": \"varchar\", \"old_field\": \"code\", \"constraints\": \"MOXI-必填-唯一,EXISTS{/plugin/api/moxi-0.0.0.1/validator/code/exists},MSG:产品名编码必须全网唯一;REG{^cp[0-9]*$}MSG:系统默认cp+数字从小到大\"}, {\"field\": \"alias\", \"label\": \"中文别名\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"多个,用逗号,顿号分割\", \"valtype\": \"varchar\", \"old_field\": \"alias\", \"constraints\": \"MOXI\"}, {\"field\": \"en\", \"label\": \"英文名称\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"en\", \"constraints\": \"MOXI\"}, {\"field\": \"alias_en\", \"label\": \"英文别名\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"alias_en\", \"constraints\": \"MOXI\"}, {\"field\": \"cas\", \"label\": \"cas号\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"分三段,全网唯一\", \"valtype\": \"varchar\", \"old_field\": \"cas\", \"constraints\": \"MOXI,REG{[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+},MSG:三字段分隔符号;\"}, {\"field\": \"molecule_express\", \"label\": \"分子式\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"molecule_express\", \"constraints\": \"MOXI\"}, {\"field\": \"molecule_num\", \"label\": \"分子量\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"molecule_num\", \"constraints\": \"MOXI,REG{^[0-9\\\\.]{1,50}$}\"}, {\"field\": \"thumbnail\", \"label\": \"化学结构式图\", \"default\": \"-\", \"inptype\": \"file\", \"options\": \"-\", \"remarks\": \"此图和现有产品缩略图合二为一，将产品缩略图省略\", \"valtype\": \"varchar\", \"old_field\": \"thumbnail\", \"constraints\": \"MOXI-必填\"}, {\"field\": \"physicochemical_property\", \"label\": \"物化性质\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"physicochemical_property\", \"constraints\": \"MOXI\"}, {\"field\": \"application\", \"label\": \"产品用途\", \"default\": \"-\", \"inptype\": \"text\", \"options\": \"-\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"application\", \"constraints\": \"MOXI\"}, {\"field\": \"is_danger\", \"label\": \"是否危险品\", \"default\": \"-\", \"inptype\": \"checkbox\", \"options\": \"-\", \"remarks\": \"-\", \"valtype\": \"int\", \"old_field\": \"is_danger\", \"constraints\": \"MOXI\"}, {\"field\": \"is_drugs\", \"label\": \"是否易制毒\", \"default\": \"-\", \"inptype\": \"checkbox\", \"options\": \"-\", \"remarks\": \"-\", \"valtype\": \"int\", \"old_field\": \"is_drugs\", \"constraints\": \"MOXI\"}, {\"field\": \"is_highly_toxic\", \"label\": \"是否剧毒化学品\", \"default\": \"-\", \"inptype\": \"checkbox\", \"options\": \"-\", \"remarks\": \"-\", \"valtype\": \"int\", \"old_field\": \"is_highly_toxic\", \"constraints\": \"MOXI\"}, {\"field\": \"up_stream_goods\", \"label\": \"上游产品\", \"default\": \"-\", \"inptype\": \"select-mul\", \"options\": \"url:/plugin/api/moxi-0.0.0.1/options/product/2\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"up_stream_goods\", \"constraints\": \"MOXI\"}, {\"field\": \"down_stream_goods\", \"label\": \"下游产品\", \"default\": \"-\", \"inptype\": \"select-mul\", \"options\": \"url:/plugin/api/moxi-0.0.0.1/options/product/2\", \"remarks\": \"\", \"valtype\": \"varchar\", \"old_field\": \"down_stream_goods\", \"constraints\": \"MOXI\"}]', '-1', '化工产品名称库', '-1' 
     * 
     */
    @Test
    public void products() {
        
        final var dataApp = new DataApp() {
            public DataApp reload() {
                return this.reload(this.cfg.add(REC("url", "jdbc:mysql://localhost:3306/salespro?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=True")));
            }// reload
        }.reload();
        
        dataApp.jdbcApply(jdbc->jdbc.sql2recordS("select * from t_pct_attribute_set")).limit(3).collect(DFrame.dfmclc).rowsS().forEach(e->{
            println(e);
        }); // 注意此时 sql2recordS 的流没有被关闭，因为采用了 limit 3 短路流
            
        dataApp.withTransaction(OPEN(DFrame.dfmclc,sess -> {
            final var dfm = sess.sql2u("select * from t_pct_attribute_set");
            final var pctattrIds = dfm.rowsS(e -> BasePair.bp(
                    e.i4("Id"), // 产品属性Id
                    REC( // 产品属性列表
                        "Owner", e.get("Owner"), 
                        "attributes", e.recs("Attributes")
                    ) // REC
                )).collect(PVec.mapclc()); // 生成 产品属性Id表
            final MgClc pctclc = new MgClc("127.0.0.1", 27017, "salespro", "t_product"); // 产品系列
            // 遍历产品
            pctclc.findRecords().stream().map(LittleTree.kvp_int()).forEach(kvp -> {
                final var i = kvp._1(); // 序号
                final var e = kvp._2(); // 元素信息
                final var pctId = e.path2str("Fields/id");
                final var en = e.path2optstr("Fields/en").map(String::strip).orElse("null");
                final var name = e.path2optstr("Fields/name").map(String::strip).orElse("空值");
                final var attrsId = e.path2int("PctAttrsId");
                final var pctattrs = pctattrIds.get(attrsId) // 提取属性列表
                    .recS("attributes").limit(3) // 提取属性明细
                    .map(r -> r.str("field") + ":" + r.str("inptype") ) // 提取属性名称
                    .collect(Collectors.toList()); // 合并成 列表形式
                println(i,pctId,en,name,attrsId,pctattrs);
            });
        })); // 注意此时 会把 jdbcApply 的 sql2recordS 的流一同给予关闭
        
        dataApp.close(); // 这个close 可以省略，因为 withTransaction 已经做了清理
        
        println("运行结束");
    }

}
